﻿//begin <{$lang.log}>分页
var paginationPageSizeLog = 10;
var paginationCurrentPageLog = 1;
var paginationTotalLog;
function pageselectCallbackLog(page_id, jq) {
    paginationCurrentPageLog = page_id + 1;
    initDataLog(page_id);
}

function changePageSizeLog(obj) {
    paginationPageSizeLog = $(obj).val();
    if (paginationPageSizeLog <= 0) {
        paginationPageSizeLog = 20;
    }
    if (Math.ceil(paginationTotalLog / paginationPageSizeLog) <= paginationCurrentPageLog) {
        paginationCurrentPageLog = Math.ceil(paginationTotalLog / paginationPageSizeLog);
    }
    if (paginationCurrentPageLog == 0) {
        paginationCurrentPageLog = 1;
    }
    initDataLog(paginationCurrentPageLog - 1);
}

function initDataLog(pageIndex) {
    loading();
    if (typeof(paginationTotalLog) != 'undefined') {
        if (Math.ceil(paginationTotalLog / paginationPageSizeLog) <= paginationCurrentPageLog) {
            paginationCurrentPageLog = Math.ceil(paginationTotalLog / paginationPageSizeLog);
            pageIndex = paginationCurrentPageLog - 1;
        }
    }
    loadDataLog(pageIndex + 1, paginationPageSizeLog);
    closeLoading();
    //加入分页的<{$lang.bind}>
    $("#paginationlog").pagination(paginationTotalLog, {
        callback: pageselectCallbackLog,
        first_text:'First',
        prev_text: '< Previous',
        next_text: 'Next >',
        last_text: 'Last',
        perpage_text: 'PerPage',
        total_text: 'Total',
        items_per_page: paginationPageSizeLog,
        num_display_entries: 6,
        current_page: pageIndex,
        num_edge_entries: 2
    });
    $("#paginationlog #pageSize").attr('onchange', 'changePageSizeLog(this)');
}
//end <{$lang.log}>分页

var paginationTotal = 1;//记录总数
var isSearch = false;
var sortColumn = "";
var sortMethod = "";
function submitsearch() {
    isSearch = true;
    initData(0);
}

function loadData(page, pageSize) {
    var vForm = "";
    if (isSearch == true && isSearch != undefined) {
        vForm = $("#sForm").serializeArray();
    }
    page=page<1?1:page;
    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/warehouse/shelf/list/page/" + page + "/pageSize/" + pageSize+"/sortColumn/"+sortColumn+"/sortMethod/"+sortMethod,
        data: vForm,
        success: function(json) {
        	$(".checkAll").attr("checked",false);
            var list = "";
            paginationTotal = json.count;
            if (json.ask == 0) {
                list = "<td colspan='10' class=\"center\">&nbsp;<{$lang.no_data}></td>";
            } else {
                var i = page == 1 ? 1 : pageSize * (page - 1) + 1;
                $.each(json.result, function(key, val) {
                    var statusIcon = val.ws_statu == 0 ? "unnormal.png" : "normal.png";
					var titleStr =  val.wa_status == 0 ? "<{$lang.unuseable}>" : "<{$lang.useable}>";
                    if ((key + 1) % 2 == 1) {
                        list += "<tr class=''>";
                    } else {
                        list += "<tr class='even-tr'>";
                    }
                    var product_sku = (val.product_sku !=null) ? val.product_sku : '';
                    var wp_quantity = val.wp_quantity != undefined ? val.wp_quantity : '&nbsp;';
                    var wp_quantity_hold = val.wp_quantity_hold != undefined ? val.wp_quantity_hold : '&nbsp;';
                    var lastdate=val.wh_last_update !=undefined ? val.wh_last_update : '&nbsp;'
                    var wp_statu_text='&nbsp;';
                   // var product_img = val.product_img != '' ? "<img src='" + val.product_img + "' style=\"width:24px;height:24px;border:0;\">" : '&nbsp;';
                    //var quantityedit = "<a href=\"javascript:editquantity(" + val.ws_id + "," + val.product_id + ")\"><img width='16' height='16' src=\"/images/icon/authority.png\"></a>&nbsp;&nbsp;|&nbsp;&nbsp;";
                    if (val.product_id == null) {
                        quantityedit = "";
                    }
                    if(val.wp_status=='0'){
                        wp_statu_text='<{$lang.release}>';
                    }else if(val.wp_status=='1'){
                        wp_statu_text='<{$lang.in_use}>';
                    }else if(val.wp_status=='2'){
                        wp_statu_text='<{$lang.pandianing}>';
                    }else if(val.wp_status=='3'){
                        wp_statu_text='<{$lang.in_use}>.';    
                    }else{
                        wp_statu_text='<{$lang.release}>';
                    }
                    list += '<td><input type="checkbox" name="ws_ids[]" class="checkShelf" value="'+val.ws_id+'"></td>';
                    list += "<td>" + (i++) + "</td>";
                    list += "<td>" + val.wa_name + "</td>";
                    list += "<td>" + val.ws_code + "</td>";
                    list += "<td>" + product_sku + "</td>";
                    list += "<td>" + wp_quantity + "</td>";
                    list += "<td>" + wp_quantity_hold + "</td>";
                    list += "<td>" + val.rate + "%</td>";
                    list += "<td>" + wp_statu_text + "</td>";
                    list += "<td>" + lastdate + "</td>";
                    list += "<td class=\"center\"><img title="+titleStr+" src=\"/images/icon/" + statusIcon + "\"></td>";
                  //  list += "<td class=\"center\"><a href=\"javascript:detail(" + val.ws_id + ")\"><img src=\"/images/icon/info.jpg\"></a></td>";
                    list += "<td class=\"center\"><a href=\"javascript:edit(" + val.ws_id + ")\"><img src=\"/images/icon/edit.gif\" title=\"<{$lang.edit}>\"></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href=\"javascript:delelteInfo(" + val.ws_id + ")\"><img src=\"/images/icon/delete.gif\" title=\"<{$lang.delete}>\"></a></td>";
                    list += "</tr>";
                });
            }
            $("#loadListData").html(list);
        }
    });
}

//<{$lang.warehouse}><{$lang.list}>
function loadSelect(act) {
    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/warehouse/shelf/select",
        data: null,
        success: function(json) {
            var list = "";
            if (json.ask == 1) {
                $.each(json.result, function(key, val) {
                    list += "<option value=" + val.warehouse_id + ">" + val.warehouse_name + "</option>";
                });
            }
            $("#s_warehouse_id").html("<option value=''><{$lang.pls}></option>"+list);
            $("#warehouse_id").html(list);
            $("#warehouse").html(list);
        }
    });
}

//<{$lang.area}><{$lang.list}>
function loadAreaSelect() {
    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/warehouse/area/select",
        data: null,
        success: function(json) {
            var list = "";
            if (json.ask == 1) {
                $.each(json.result, function(key, val) {
                    list += "<option value=" + val.wa_code + ">" + val.wa_name + "</option>";
                });
                $("#wa_code").html(list);
                $("#room_wa_code").html(list);
                $("#s_wa_code").html("<option value=''><{$lang.pls}></option>"+list);
            }
        }
    });
}

//房间<{$lang.list}>
function loadHouseSelect() {
    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/warehouse/house/selectlist",
        data: null,
        success: function(json) {
            var list = "";
            if (json.ask == 1) {
                $.each(json.result, function(key, val) {
                    list += "<option value=" + val.wh_code + ">" + val.wh_code + "</option>";
                });
                $("#wh_code").html(list);
                $("#room_number").html(list);
            }
        }
    });
}

//<{$lang.delete}>
function delelteInfo(id) {
    if (id == '' || id == undefined) {
        return false;
    }
    Uid = id;
    $('#right_confirm').dialog('open');
}

function dialogclose() {
    $("#dialog").dialog("close");
}

$(function() {
//    initData(0);
   // loadSelect('search');//加载<{$lang.warehouse}>
   // loadSelect();//<{$lang.warehouse}>
   // loadAreaSelect(); //<{$lang.area}>
    //loadHouseSelect();//房间
    $("#dialog").dialog({
        autoOpen:false,
        modal:true,
        width:380,
        show:"slide",
        buttons: {
            '<{$lang.ok}>': function() {
                create();
            },
            '<{$lang.cancel}>': function() {
                $(this).dialog('close');
            }
        },
        close: function() {
            tip('');//<{$lang.clear}><{$lang.tip}><{$lang.msg}>
            $("[name=warehouse_id]").show().parent().find("span").hide();
            //$("[name=wh_code]").show().parent().find("span").hide();
            $("[name=wa_code]").show().parent().find("span").hide();
            $("[name=ws_code]").show().parent().find("span").hide();
            $('#createForm')[0].reset();
            $("[name=ws_id]").val('');
        }
    });

    $("#dialog-tabs").dialog({
        autoOpen:false,
        modal:true,
        width:680,
        show:"slide"
    });

    //<{$lang.delete}>
    $("#right_confirm").dialog({
        autoOpen:false,
        modal:true,
        height:160,
        show:"slide",
        buttons: {
            '<{$lang.ok}>': function() {
                $.ajax({
                    type: "post",
                    async: false,
                    dataType: "json",
                    url: "/warehouse/shelf/delete",
                    data: "ws_id=" + Uid,
                    success: function(json) {
                        if (json == null) {
                            return;
                        }
                        if (json.ask == '1') {
                            paginationTotal = paginationTotal - 1;
                            initData(paginationCurrentPage - 1);
                            messags = "<{$lang.success}><{$lang.delete}>！";
                        } else {
                            messags = "<{$lang.delete}><{$lang.fail}>！";
                        }
                        alertTip(messags);
                    }
                });
                $(this).dialog('close');
            },
            '<{$lang.cancel}>': function() {
                $(this).dialog('close');
            }
        }

    });

    //<{$lang.add}>
    $("#create").click(function() {
        tip('');
        $("#dialog").dialog({title:'<{$lang.add}><{$lang.ws_code}>',autoOpen:true});
    });
    $("#close").click(function() {
        dialogclose();
    });

    //<{$lang.sort}>
    $(".sort").click(function(){
        sortColumn = $(this).attr("sortColumn");
        sortMethod = $(this).attr("sortMethod");
        $(".sort").children(".up").remove();
        if(sortMethod == 'asc'){
            $(this).attr("sortMethod","desc");
            $(this).append("<span class='up'>↑</span>");
        }else{
            $(this).attr("sortMethod","asc");
            $(this).append("<span class='up'>↓</span>");
        }

        initData(paginationCurrentPage-1);
    });

    $("#setshelf_dialog").dialog({
        autoOpen:false,
        modal:true,
        width:560,
        show:"slide",
        buttons:{
            'close/<{$lang.close}>':function () {
                $(this).dialog('close');
            }
        }
    });
    $(".checkAll").live("click", function() {
		if (typeof $(".checkAll").attr("checked") == "string") {
			$(".checkShelf").attr("checked", true);
		} else {
			$(".checkShelf").attr("checked", false);
		}

	});
});

function create() {
    vForm = "";
    vForm = $("#createForm").serializeArray();
    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/warehouse/shelf/create",
        data: vForm,
        success: function(json) {
            if (json == null)return;
            if (json.ask != '0') {
                if (json.ask == '1') {
                    initData(0);
                } else {
                    loadData(paginationCurrentPage, paginationPageSize);
                }
                alertTip(json.msg);
                dialogclose();
            } else {
                if (json.error == null)return;
                var html = '';
                $.each(json.error, function(key, val) {
                    html += '<span><img src="/images/no.gif">' + val + '</span>';
                })
                tip(html);
            }
        }
    });
}

//<{$lang.log}>
var wsl_type = 0;//<{$lang.log}><{$lang.type}>
var ws_id = 0;
function switchTab(id) {
    wsl_type = id - 1;
    if (id == 1) {
        initDataLog(0);
    } else if (id == 2) {
        initDataLog(0);
    }
    for (var i = 1; i <= 2; i++) {
        if (i == id) {
            $("#tabs-li-" + i).addClass('tabs-head-active');
        } else {
            $("#tabs-li-" + i).removeClass('tabs-head-active');
        }
    }
}

function loadDataLog(page, pageSize) {
    loading();
    var ws_code = $('#selected_ws_code').val();
    if (ws_code == null) {
        ws_code = '0';
    }
    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/warehouse/shelf/getshelflog/page/" + page + "/pageSize/" + pageSize + "/wsl_type/" + wsl_type + "/ws_code/" + ws_code,
        data:null,
        success: function(json) {
            closeLoading();
            var list = "";
            paginationTotalLog = json.count;
            if (json.ask == 0) {
                list = "<div align='center'><{$lang.no_data}></div>";
            } else {
                var i = page == 1 ? 1 : pageSize * (page - 1) + 1;
                $.each(json.result, function(key, val) {
                    list += "<div class='slcolumn1'>" + val.wsl_id + "</div>";
                    list += "<div class='slcolumn2'>" + val.user_id + "</div>";
                    list += "<div class='slcolumn3'>" + val.wsl_create_date + "</div>";
                    list += "<div class='slcolumn4'>" + val.wsl_note + "</div>";
                    list += "<div class='myClear'></div>";
                });
            }
            $("#shelfLogData").html(list);
        }
    });
}

//<{$lang.ws_code}><{$lang.detail}>
function detail(id) {
    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/warehouse/shelf/info",
        data: "ws_id=" + id,
        success: function(json) {
            if (json == null) {
                return;
            }
            if (json.ask == 1) {
                var ws = json.result;
                $('#selected_ws_id').val(json.result.ws_id);
                $('#selected_ws_code').val(json.result.ws_code);
                $(".bill-item-right").each(function() {
                    var ref = $(this).attr('ref');
                    if (ref == 'ws_statu') {
                        var icon = ws[ref] == 0 ? "unnormal.gif" : "normal.gif";
                        ws[ref] = '<img src="/images/icon/' + icon + '" style="margin-top: 9px;"/>';
                    }
                    $(this).html('<span>' + ws[ref] + '</span>');
                });
                switchTab(1);//加载<{$lang.log}>
                $("#dialog-tabs").dialog("open");
            }
        }
    });
}
//<{$lang.edit}><{$lang.ws_code}>
function edit(id) {
    tip('');//<{$lang.clear}><{$lang.tip}><{$lang.msg}>
    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/warehouse/shelf/info",
        data: "ws_id=" + id,
        success: function(json) {
            if (json == null) {
                return;
            }
            if (json.ask == 1) {
                $("[name=warehouse_id]").hide().parent().find("span").text(json.result.warehouse_name).show();
                $("[name=warehouse_id]").val(json.result.warehouse_id);
                //$("[name=wh_code]").hide().parent().find("span").text(json.result.wh_code).show();
                $("[name=wa_code]").hide().parent().find("span").text(json.result.wa_code).show();
                $("[name=wa_code]").val(json.result.wa_code);
                $("[name=ws_code]").hide().parent().find("span").text(json.result.ws_code).show();
                //$("[name=wh_code]").val(json.result.wh_code);
                $("[name=ws_code]").val(json.result.ws_code);
                //$("[name=wa_code]").val(json.result.wa_code);
                $("[name=ws_note]").val(json.result.ws_note);
                $("[name=wsc_code]").val(json.result.wsc_code);
                $("[name=ws_sort]").val(json.result.ws_sort);
                $("[name=ws_statu]").val(json.result.ws_statu);
                $("[name=ws_id]").val(json.result.ws_id);
                $("#dialog").dialog({title:'<{$lang.edit}><{$lang.ws_code}>',autoOpen:true});
            }
        }
    });
}
//<{$lang.batch}><{$lang.create}><{$lang.ws_code}>
function batchshelf() {
    //loadSelect();
    $('#batchForm')[0].reset();
    $('#batch_shelf_form').dialog({
        width: 550,
        modal: true,
        show:"slide",
        buttons: {
            '<{$lang.submit}><{$lang.view}>': function() {
                batchtip('');
                viewshelf();
            },
            '<{$lang.ok}>': function() {
                batchtip('');
                httpRequest = $.post('/warehouse/shelf/batch', $('#batchForm').serializeArray(), batchCallback, 'json');
            },
            '<{$lang.cancel}>': function() {
                $(this).dialog('close');
            }
        },
        close: function() {
            batchtip('');
            $('#batchForm')[0].reset();
        }
    });
}
function batchCallback(data) {
    if (data == null) return null;
    if (data.ask != 1) {
        if (data.error == null)return;
        var html = '';
        $.each(data.error, function(key, val) {
            html += '<span><img src="/images/no.gif">' + val + '</span>';
        })
        batchtip(html);
    } else {
        $('#batch_shelf_form').dialog("close");
        alertTip(data.msg);
        initData(0);
    }
}

function tip(mess) {
    var fid = $("#tips");
    fid.html(mess);
    if (mess != '') {
        fid.show();
    } else {
        fid.hide();
    }
}

function batchtip(mess) {
    var fid = $("#batchtips");
    fid.html(mess);
    if (mess != '') {
        fid.show();
    } else {
        fid.hide();
    }
}

function quantitytips(mess) {
    var fid = $("#quantity_tips");
    fid.html(mess);
    if (mess != '') {
        fid.show();
    } else {
        fid.hide();
    }
}
//<{$lang.operation}><{$lang.tip}>
function alertTip(tip) {
    $('<div title="<{$lang.tip}>"><p align="center">' + tip + '</p></div>').dialog({
        modal: true,
        width: 500,
        buttons: {
            '<{$lang.ok}>': function() {
                $(this).dialog("close");
            }
        }
    });
}

//<{$lang.view}><{$lang.ws_code}>
function viewshelf() {
    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/warehouse/shelf/batch/view/1",
        data:$('#batchForm').serializeArray() ,
        success: function(json) {
            if (json == null) {
                return;
            }
            if (json.ask == 1) {
                var ws = json.result;
                var html = '';
                $.each(ws, function(key, val) {
                    html += '<li>' + val + '</li>';
                })
                $("#div_ws_code").html(html);
                $("#div_total").html(json.total);
                $("#setshelf_dialog").dialog({title:'<{$lang.create}><{$lang.ws_code}><{$lang.view}>',autoOpen:true});
            } else{
                if (json.error == null)return;
                var html = '';
                $.each(json.error, function(key, val) {
                    html += '<span><img src="/images/no.gif">' + val + '</span>';
                })
                batchtip(html);
            }
        }
    });
}
//<{$lang.operation}><{$lang.tip}>
function alertTip(tip) {
    $('<div title="<{$lang.tip}>(<{$lang.close}><{$lang.esc}>)"><p align="center">' + tip + '</p></div>').dialog({
    	width: 300,
        modal: true,     
        show:"slide",
        buttons: {           
            '<{$lang.close}>': function() {              
                $(this).dialog('close');
            }
        },
        close:function(){
        	
        }
    });
}

function batchPrint(type){
	if($(".checkShelf:checked").size()<1){
		alertTip("<{$lang.pls}><{$lang.ws_code}>");
		return false;
	}
	if(type){
		$("#printType").val(type);
	}
	$("#formPrint").submit();
	
}

$(function(){
    $("#printDialog").dialog({
        autoOpen : false,
        modal : true,
        width : 650,
        show : "slide",
        buttons : {
            "OK" : function() {
                $("#PrintProductForm").submit();
                $("#printDialog").dialog("close");
                $(".checkShelf").attr("checked", false);
                $(".checkAll").attr("checked", false);  
            },
            "Close" : function() {
                $(this).dialog("close");
            }
        }
    });
});
function PrintProduct(printType) {
    if ($(".checkShelf:checked").size()) {
        var vForm = $("[name='ws_ids[]']","#formPrint").serialize();
        $.ajax({
            url: "/warehouse/shelf/get-shelf-product",
            async: false,
            dataType: "json",
            type:"post",
            data:vForm,
            success: function(json) {               
                if(json.ask=='1'){
                	 var html = "<form action='/warehouse/shelf/print-product-label' method='post' id='PrintProductForm' target='_blank'>";
					 html += '<input type="hidden" name="type" value="'+printType+'">';
                     html += '<table class="myTab" width="100%" cellspacing=3 cellpadding=3  border=1>';
                     html += '<tr >';
                     html += '<th ><{$lang.ws_code}></th>';
                     html += '<th ><{$lang.sku}></td>';
                     html += '<th >Category</th>';
                     html += '<th class="no-background">Qty</th>';
                     html += '</tr>';
                    $.each(json.result,function(i,val){
                        html += '<tr>';
                        html += '<td>'+val.ws_code+'</td>';
                        html += '<td>'+(val.customer_id + '-' +val.product_sku)+'</td>';
                        html += '<td>'+val.category+'</td>';
                        html += '<td><input type="text" class="width1" value="'+val.quantity+'" size=3 name="'+val.ws_code+'#productid#'+val.product_id+'"></td>';
                        html += '</tr>';
                    });

                    html += '</table>';
                    html += "</form>";
                    $("#printDialog").html(html).dialog("open");
                }else{
                	alertTip("<{$lang.print}><{$lang.ws_code}><{$lang.exception}><{$lang.product}><{$lang.not_exists}>");
                }
            }
        });

    } else {
        alertTip("<{$lang.pls}><{$lang.ws_code}>");
        return false;
    }
}